/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface;

import embeddedpaint.model.layers.LayerManager;
import embeddedpaint.model.layers.ALayer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class PanelListOfLayer extends JPanel implements Observer, ListSelectionListener, MouseListener
{
    private static final Dimension  M_PREFERED_SIZE                     = new Dimension(150, 10);
    private static final String     M_LABEL_PANEL_LIST_OF_LAYER         = "List of Layers :";
    private static final int        M_SELECTION_MODE                    = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
    private static final int        M_MOUSE_BUTTON_TO_OPEN_POPUP_MENU   = MouseEvent.BUTTON3;
    
    private LayerManager            m_list;
    private JList                   m_jList;
    private int                     m_dragIndex;
    private PopupMenuForLayers      m_popupMenu;

    public PanelListOfLayer (LayerManager list)
    {
        this.setPreferredSize(M_PREFERED_SIZE);
        
        this.m_list = list;
        this.m_list.addObserver(this);

        this.m_jList = new JList();
        this.m_jList.setSelectionMode        (M_SELECTION_MODE);
        this.m_jList.setCellRenderer         (new ImageListCellRenderer());
        this.m_jList.addListSelectionListener(this);
        this.m_jList.addMouseListener        (this);

        this.setLayout(new BorderLayout());
        
        this.add(new Label(M_LABEL_PANEL_LIST_OF_LAYER), BorderLayout.NORTH);
        this.add(this.m_jList, BorderLayout.CENTER);

        this.m_popupMenu = new PopupMenuForLayers(this.getParent(), this.m_list);

        this.refreshList();
    }

    public void refreshList ()
    {
        this.m_jList.setListData(this.m_list.getM_imageList().toArray());
    }

    @Override
    public void update (Observable o, Object arg)
    {
        String updateKey = (String) arg;

        if (updateKey.equals(LayerManager.G_UPDATE_KEY_ADD_LAYER))
        {
            this.refreshList();
        }
    }

    @Override
    public void valueChanged (ListSelectionEvent e)
    {
        ArrayList   layers          = this.m_list.getM_imageList();
        int[]       indicesSlected  = this.m_jList.getSelectedIndices();

        ArrayList selectedLayers = new ArrayList();

        for (int i : indicesSlected)
        {
            selectedLayers.add(layers.get(i));
        }

        this.m_list.setM_selectedImage(selectedLayers);
    }

    @Override
    public void mouseClicked (MouseEvent e)
    {
        if (e.getButton() == M_MOUSE_BUTTON_TO_OPEN_POPUP_MENU)
        {
            int releaseIndex = this.m_jList.locationToIndex(e.getPoint());

            if (releaseIndex != -1)
            {
                ALayer img = this.m_list.get(releaseIndex);
                
                this.m_popupMenu.setLayer(img);
                this.m_popupMenu.show    (this, e.getX(), e.getY());
            }
        }
    }

    @Override
    public void mousePressed (MouseEvent e)
    {
        m_dragIndex = this.m_jList.locationToIndex(e.getPoint());
    }

    @Override
    public void mouseReleased (MouseEvent e)
    {
        int releaseIndex = this.m_jList.locationToIndex(e.getPoint());

        if (releaseIndex != this.m_dragIndex && releaseIndex != -1)
        {
            this.m_list.move(this.m_dragIndex, releaseIndex);
        }
    }

    @Override
    public void mouseEntered (MouseEvent e)
    {
    }

    @Override
    public void mouseExited (MouseEvent e)
    {
    }
}
